<?php

class EmpleatController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->view->title = "Empleats";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $request = $this->getRequest();
        if ($request->getParam('model')) {
            $nModel = $request->getParam('model');
        } else {
            $nModel = 'natives';
            $this->view->message = "Ja que no s'ha seleccionat un model, per defecte.. Natives";
        }
        $model = Application_Model_NouModel::crearModel($nModel);
        if ($model != null) {
            $this->view->llistat = $model->listEmps();
        }
    }

    public function afegirAction() {
        $this->view->title = "Nou empleat";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $request = $this->getRequest();

        if ($request->getParam('model')) {
            $nModel = $request->getParam('model');
        } else {
            $nModel = 'natives';
            $this->view->message = "Ja que no s'ha seleccionat un model, per defecte.. Natives";
        }
        $model = Application_Model_NouModel::crearModel($nModel);
        $depts = $model->listDeptsSelect();

        $form = new Application_Form_Empleat($depts);
        $this->view->form = $form;

        if ($request->isPost()) {
            $formData = $request->getPost();
            if ($form->isValid($formData)) {
                $f = $form->getValues();
                $model->addEmp($f['nom'], $f['cognom'], $f['dni'], $f['departament']);
                if ($request->getParam('ajax')) {
                    $this->_redirect('/index/ajax');
                } else {
                    $this->_redirect('/empleat/index/model/' . $nModel);
                }
            }
        }
    }

    public function editarAction() {
        $this->view->title = "Editant empleat";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $request = $this->getRequest();

        if ($request->getParam('model')) {
            $nModel = $request->getParam('model');
        } else {
            $nModel = 'natives';
            $this->view->message = "Ja que no s'ha seleccionat un model, per defecte.. Natives";
        }
        $model = Application_Model_NouModel::crearModel($nModel);
        $depts = $model->listDeptsSelect();

        $form = new Application_Form_Empleat($depts);
        $this->view->form = $form;
        if ($request->getParam('id')) {
            $idEmp = $request->getParam('id');
            if ($request->isPost()) {
                $formData = $request->getPost();
                if ($form->isValid($formData)) {
                    $f = $form->getValues();
                    $model->editEmp($idEmp, $f['nom'], $f['cognom'], $f['dni'], $f['departament']);
                    if ($request->getParam('ajax')) {
                        $this->_redirect('/index/ajax');
                    } else {
                        $this->_redirect('/empleat/index/model/' . $nModel);
                    }
                }
            } else {
                $dades = $model->getEmp($idEmp);
                $form->populate($dades);
            }
        } else {
            //Si no hi ha l'id d'emp. El redirigim a l'index.
            $this->_redirect('/');
        }
    }

    public function eliminarAction() {
        $request = $this->getRequest();
        if ($request->getParam('id')) {
            if ($request->getParam('model')) {
                $nModel = $request->getParam('model');
            } else {
                $nModel = 'natives';
                $this->view->message = "Ja que no s'ha seleccionat un model, per defecte.. Natives";
            }
            $model = Application_Model_NouModel::crearModel($nModel);
            $model->deleteEmp($request->getParam('id'));
            if ($request->getParam('ajax')) {
                $this->_redirect('/index/ajax');
            } else {
                $this->_redirect('/empleat/index/model/' . $nModel);
            }
        }
    }

}

